#!/bin/bash

set -e  # 如果任何命令失败，脚本将立即退出

# 定义安装路径
INSTALL_PREFIX="/usr/local"
SOURCE_DIR="/home/depends"  # 新的源码存放目录，可以修改为其他路径

# 创建源码存放目录
mkdir -p ${SOURCE_DIR}

# 1. 安装 Hiredis
install_hiredis() {
    echo "Installing Hiredis..."
    # 下载 Hiredis 源码
    if [ ! -d "${SOURCE_DIR}/hiredis" ]; then
        git clone https://github.com/redis/hiredis.git ${SOURCE_DIR}/hiredis
    else
        echo "Hiredis source already exists, pulling latest changes..."
        cd ${SOURCE_DIR}/hiredis
        git pull
        cd ..
    fi
    cd ${SOURCE_DIR}/hiredis

    # 编译和安装
    mkdir -p build && cd build
    cmake .. -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX}
    make -j$(nproc)
    sudo make install
    cd ../..
    echo "Hiredis installed successfully."
}

# 2. 安装 Redis++
install_redispp() {
    echo "Installing Redis++..."
    # 下载 Redis++ 源码
    if [ ! -d "${SOURCE_DIR}/redis-plus-plus" ]; then
        git clone https://github.com/sewenew/redis-plus-plus.git ${SOURCE_DIR}/redis-plus-plus
    else
        echo "Redis++ source already exists, pulling latest changes..."
        cd ${SOURCE_DIR}/redis-plus-plus
        git pull
        cd ..
    fi
    cd ${SOURCE_DIR}/redis-plus-plus

    mkdir -p build && cd build
    cmake .. -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} \
             -DHIREDIS_ROOT=${INSTALL_PREFIX} \
             -DREDIS_PLUS_PLUS_CXX_STANDARD=11  # 使用 C++11 编译
    make -j$(nproc)
    sudo make install
    cd ../..
    echo "Redis++ installed successfully."
}

# 3. 安装 TA-Lib
install_talib() {
    echo "Installing TA-Lib..."
    # 下载 TA-Lib 源码
    if [ ! -d "${SOURCE_DIR}/ta-lib" ]; then
        git clone https://github.com/TA-Lib/ta-lib.git ${SOURCE_DIR}/ta-lib
    else
        echo "TA-Lib source already exists, pulling latest changes..."
        cd ${SOURCE_DIR}/ta-lib
        git pull
        cd ..
    fi
    cd ${SOURCE_DIR}/ta-lib

    # 为 autogen.sh 添加执行权限
    chmod +x autogen.sh

    # 确保 libtool 已安装
    sudo apt-get install -y libtool

    # 编译和安装
    ./autogen.sh
    ./configure --prefix=${INSTALL_PREFIX}
    make -j$(nproc)
    sudo make install
    cd ..
    echo "TA-Lib installed successfully."
}

# 4. 安装 Qt 5.15 开发环境
install_qt5() {
    echo "Installing Qt 5.15..."
    sudo apt-get update
    sudo apt-get install -y \
        qt5-qmake qtbase5-dev qtchooser qt5-qmake-bin qtbase5-dev-tools \
        qtdeclarative5-dev libqt5svg5-dev libqt5websockets5-dev \
        libqt5x11extras5-dev
    echo "Qt 5.15 installed successfully."
}

# 5. 更新动态库路径
update_ldconfig() {
    echo "Updating shared library cache..."
    sudo ldconfig
}

# 6. 验证安装
verify_install() {
    echo "Verifying installation..."

    # 验证 Hiredis
    if [ -f "${INSTALL_PREFIX}/lib/libhiredis.so" ]; then
        echo "Hiredis installed successfully!"
    else
        echo "Hiredis installation failed."
    fi

    # 验证 Redis++
    if [ -f "${INSTALL_PREFIX}/lib/libredis++.so" ]; then
        echo "Redis++ installed successfully!"
    else
        echo "Redis++ installation failed."
    fi

    # 验证 TA-Lib
    if [ -f "${INSTALL_PREFIX}/lib/libta_lib.so" ]; then
        echo "TA-Lib installed successfully!"
    else
        echo "TA-Lib installation failed."
    fi

    # 验证 Qt 5.15
    if qmake --version | grep -q "Qt version 5.15"; then
        echo "Qt 5.15 installed successfully!"
    else
        echo "Qt 5.15 installation failed."
    fi
}

# 主流程
main() {
    echo "Starting installation process for Hiredis, Redis++, and TA-Lib..."
    install_hiredis
    install_redispp
    install_talib
    install_qt5
    update_ldconfig
    verify_install
    echo "Installation process completed!"
}

main
